/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.*;
import com.cashmate.hibernate.pojo.*;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Lee
 */
public class GetHistory extends ActionSupport {

    // private BranchDAO brd = (BranchDAO) ServletActionContext.getServletContext().getAttribute("branchDAO");
    //private LoanapplicationDAO lad = (LoanapplicationDAO) ServletActionContext.getServletContext().getAttribute("loanapplicationDAO");
    private BorrowerDAO bord = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    // private MatrixcategoriesCriteriaDAO mccd = (MatrixcategoriesCriteriaDAO) ServletActionContext.getServletContext().getAttribute("matrixcategoriesCriteriaDAO");
    // private MatrixcategoriesDAO mcd = (MatrixcategoriesDAO) ServletActionContext.getServletContext().getAttribute("matrixcategoriesDAO");
    //private MatrixcriteriaDAO mcrd = (MatrixcriteriaDAO) ServletActionContext.getServletContext().getAttribute("matrixcriteriaDAO");
    //private CreditinvestigationDAO cd = (CreditinvestigationDAO) ServletActionContext.getServletContext().getAttribute("creditinvestigationDAO");
    //private BorrowerinfoDAO bid = (BorrowerinfoDAO) ServletActionContext.getServletContext().getAttribute("borrowerinfoDAO");
    private LoansDAO ld = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private BorrowerDAO bo = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private LoansDAO loanDAO = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
//    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private String bcode;
    private String firstname, middlename, lastname;
    private String cat1;
    private Borrowers bor;

    public String getCat1() {
        return cat1;
    }

    public void setBcode(String bcode) {
        this.bcode = bcode;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setMiddlename(String middlename) {
        this.middlename = middlename;
    }
    
    @Action(value = "/get-history", results = {
        @Result(name = "input", location = "${cat1}"),
        @Result(name = "success", location = "${cat1}"),
//        
        @Result(name = "error", location = "${cat1}")
    })
    @Override
    public String execute() {
        cat1 = "../../loanverification.jsp";

        List<Borrowers> allbors = null;
        boolean hasMatch = false;
        try {
            if (!bcode.isEmpty()) {
                allbors = bo.getAll();
                for (int i = 0; i < allbors.size(); i++) {
                    bor = allbors.get(i);
                    String borcode = allbors.get(i).getBorrowercode();
                    Loans latestloan = null;
                    Loanverification latestloanver = null;
                    if (borcode.equals(bcode)) {
                        try {
                            latestloan = loanDAO.getLatestLoansByBorrower(bor.getBorrowerid());
                            latestloanver = lvd.getLatestloanver(bor.getBorrowerid());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        hasMatch = true;
                    }

                    if (hasMatch == true) {
                        ServletActionContext.getRequest().setAttribute("borrowerid", bor.getBorrowerid());
                        ServletActionContext.getRequest().setAttribute("borrowerinfo", bor);
                        ServletActionContext.getRequest().setAttribute("latestloan", latestloan);
                        ServletActionContext.getRequest().setAttribute("latestloanver", latestloanver);
                        return SUCCESS;
                    }
                }

                if (hasMatch == false) {
                    ServletActionContext.getRequest().setAttribute("borrowerid", null);
                    ServletActionContext.getRequest().setAttribute("borrowerinfo", null);
                    ServletActionContext.getRequest().setAttribute("latestloan", null);
                    ServletActionContext.getRequest().setAttribute("latestloanver", null);
                    return SUCCESS;
                }
            } else if (!firstname.isEmpty() && !middlename.isEmpty() && !lastname.isEmpty()) {
                allbors = bo.getAll();
                for (int i = 0; i < allbors.size(); i++) {
                    bor = allbors.get(i);
                    String borcode = allbors.get(i).getBorrowercode();
                    Loans latestloan = null;
                    Loanverification latestloanver = null;
                    if (firstname.equals(bor.getFirstname()) && middlename.equals(bor.getMiddlename()) && lastname.equals(bor.getLastname())) {

                        try {
                            latestloan = loanDAO.getLatestLoansByBorrower(bor.getBorrowerid());
                            latestloanver = lvd.getLatestloanver(bor.getBorrowerid());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

//                        ServletActionContext.getRequest().setAttribute("borrowerid", latestloan.getBorrowers().getBorrowerid().toString());
                        hasMatch = true;
                    }

                    if (hasMatch == true) {
                        ServletActionContext.getRequest().setAttribute("borrowerid", bor.getBorrowerid());
                        ServletActionContext.getRequest().setAttribute("borrowerinfo", bor);
                        ServletActionContext.getRequest().setAttribute("latestloan", latestloan);
                        ServletActionContext.getRequest().setAttribute("latestloanver", latestloanver);
                        return SUCCESS;
                    }
                }

                if (hasMatch == false) {
                    ServletActionContext.getRequest().setAttribute("borrowerid", null);
                    ServletActionContext.getRequest().setAttribute("borrowerinfo", null);
                    ServletActionContext.getRequest().setAttribute("latestloan", null);
                    ServletActionContext.getRequest().setAttribute("latestloanver", null);
                    return SUCCESS;
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return SUCCESS;
    }
}
